package com.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.test.mapper.UserMapperPlus;
import com.test.util.MyBatisPlusUtil;
import com.test.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;
import com.test.entity.User;

import java.util.List;

public class UserMapperPlusTest {
    private SqlSession sqlSession;

    private UserMapperPlus userMapperPlus;

    @Before
    public void setUp() {
        // 创建测试表
//        DatabaseUtil.createTestTable();

        // 获取SqlSession和Mapper
        sqlSession = MyBatisPlusUtil.getSqlSession();
        userMapperPlus = sqlSession.getMapper(UserMapperPlus.class);

        System.out.println("🚀 开始执行MyBatis CRUD测试...");
    }
    @Test
    public void createUser(){
        User user=new User("李四","123@qq.com",20);
        userMapperPlus.insert(user);
        System.out.println("创建用户成功");
    }
    @Test
    public void list(){
        QueryWrapper<User>wrapper=new QueryWrapper<>();
        wrapper.eq("name","李四");
        wrapper.ne("name","李四");
        wrapper.select("id","name");
        wrapper.orderByDesc("id");
        List<User>userList=userMapperPlus.selectList(wrapper);
        userList.forEach(user ->{
            System.out.println(user);
        });
    }
    @Test
    public void update(){
        UpdateWrapper<User> wrapper=new UpdateWrapper<>();
        wrapper.eq("id","4");
        wrapper.set("name","王五");
        userMapperPlus.update(new User(),wrapper);
    }
    @Test
    public void delete(){
        userMapperPlus.deleteById(4);
    }

}
